﻿using System;
using System.Windows.Forms;
using mki.Backup.GUI.Properties;
using mki.Backup.Objects;

namespace mki.Backup.GUI.Helper
{
	/// <summary>
	/// Represents the JobLoadExtension class
	/// </summary>
	internal static class JobLoadExtension
	{
		#region Methods (4)

		// Public Static Methods (4) 

		/// <summary>
		/// Loads the DB backup jobs.
		/// </summary>
		/// <param name="backupJob">The backup job.</param>
		/// <returns>returns a object of type TreeNode</returns>
		public static TreeNode LoadDBBackupJobs(this BackupJobElement backupJob)
		{
			var backupNode = CreateTreeNode(Resources.NodeDBBackupJobs, "database_save", backupJob.DBBackupJobsCollection);

			foreach (DBBackupElement dbBackupJob in backupJob.DBBackupJobsCollection)
			{
				backupNode.Nodes.Add(CreateTreeNode(dbBackupJob.Name, "database", dbBackupJob));
			}

			return backupNode;
		}

		/// <summary>
		/// Creates the tree node.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="imageKey">The image key.</param>
		/// <param name="tag">The tag.</param>
		/// <returns></returns>
		public static TreeNode CreateTreeNode(String text, String imageKey, Object tag)
		{
			return new TreeNode(text) { ImageKey = imageKey, SelectedImageKey = imageKey, Tag = tag };
		}

		/// <summary>
		/// Loads the FTP jobs.
		/// </summary>
		/// <param name="backupJob">The backup job.</param>
		/// <returns>returns a object of type TreeNode</returns>
		public static TreeNode LoadFTPJobs(this BackupJobElement backupJob)
		{
			var ftpNode = CreateTreeNode(Resources.NodeFTPJobs, "drive_network", backupJob.FTPJobsCollection);

			foreach (FTPJobElement ftpJob in backupJob.FTPJobsCollection)
			{
				var ftpJobNode = CreateTreeNode(ftpJob.Name, "package", ftpJob);

				foreach (FTPAddElement file in ftpJob.AddFileCollection)
				{	// upload tasks
					ftpJobNode.Nodes.Add(CreateTreeNode(file.Name, "page_white_get", file));
				}

				foreach (FTPDeleteElement file in ftpJob.DeleteFileCollection)
				{	// delete tasks
					ftpJobNode.Nodes.Add(CreateTreeNode(file.Name, "page_white_delete", file));
				}

				ftpNode.Nodes.Add(ftpJobNode);
			}

			return ftpNode;
		}

		/// <summary>
		/// Loads the run process jobs.
		/// </summary>
		/// <param name="backupJob">The backup job.</param>
		/// <returns>returns a object of type TreeNode</returns>
		public static TreeNode LoadRunProcessJobs(this BackupJobElement backupJob)
		{
			var runProcessNode = CreateTreeNode(Resources.NodeRunProcessJobs, "cog", backupJob.RunProcessJobsCollection);

			foreach (RunProcessElement runProcessJob in backupJob.RunProcessJobsCollection)
			{
				runProcessNode.Nodes.Add(CreateTreeNode(runProcessJob.Name, "cog_go", runProcessJob));
			}
			return runProcessNode;
		}

		/// <summary>
		/// Loads the zip jobs.
		/// </summary>
		/// <param name="backupJob">The backup job.</param>
		/// <returns>returns a object of type TreeNode</returns>
		public static TreeNode LoadZipJobs(this BackupJobElement backupJob)
		{
			var zipNode = CreateTreeNode(Resources.NodeZipJobs, "compress", backupJob.ZipJobsCollection);

			foreach (ZipJobElement zipJob in backupJob.ZipJobsCollection)
			{
				var zipJobNode = CreateTreeNode(zipJob.Name, "folder_page_white", zipJob);

				foreach (ZipAddFileElement file in zipJob.AddFileCollection)
				{	// files
					zipJobNode.Nodes.Add(CreateTreeNode(file.Name, "page_white_zip", file));
				}
				foreach (ZipAddFolderElement folder in zipJob.AddFolderCollection)
				{	// folders
					zipJobNode.Nodes.Add(CreateTreeNode(folder.Name, "folder", folder));
				}

				zipNode.Nodes.Add(zipJobNode);
			}
			return zipNode;
		}

		#endregion Methods
	}
}